package gogogo.liwushuo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.FirstBanner2Bean;
import gogogo.liwushuo.listener.OnItemDigestClickListener;
import gogogo.liwushuo.listener.OnItemFvClickListener;
import gogogo.liwushuo.listener.OnItemIconClickListener;
import gogogo.liwushuo.listener.OnItemImageClickListener;
import gogogo.liwushuo.listener.OnItemLikeCountClickListener;
import gogogo.liwushuo.listener.OnItemLikeIconClickListener;
import gogogo.liwushuo.listener.OnItemLlayoutClickListener;
import gogogo.liwushuo.listener.OnItemNameClickListener;
import gogogo.liwushuo.listener.OnItemTitleClickListener;

/**
 * Created by chenjun on 2016/7/22.
 */
public class FirstBannerIntentAdapter extends RecyclerView.Adapter<FirstBannerIntentAdapter.Holder> {

    private List<FirstBanner2Bean.DataBean.PostsBean> list;
    private Context mContext;

    public FirstBannerIntentAdapter(List<FirstBanner2Bean.DataBean.PostsBean> list,Context context){
        this.mContext = context;
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home_recycler, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
//        Log.i("mtag", "onBindViewHolder: ");
        FirstBanner2Bean.DataBean.PostsBean bean = list.get(position);
        holder.tvCategory.setText(bean.getColumn().getCategory());
        holder.tvDigest.setText(bean.getTitle());
        holder.tvLikecCount.setText(bean.getLikes_count() + "");
        holder.tvName.setText(bean.getAuthor().getNickname());
        holder.tvTitle.setText(bean.getColumn().getTitle());
        String cover_image_url = bean.getCover_image_url();
        Picasso.with(mContext)
                .load(cover_image_url)
                .tag(cover_image_url)
                .placeholder(R.color.textcolor_unchecked)
                .error(R.color.textcolor_unchecked)
                .into(holder.ivImage);
        Picasso.with(mContext)
                .load(bean.getAuthor().getAvatar_url())
                .tag(cover_image_url)
                .placeholder(R.color.textcolor_unchecked)
                .error(R.color.textcolor_unchecked)
                .into(holder.ivIcon);
        setmHolderListener(holder,position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class Holder extends RecyclerView.ViewHolder{
        private TextView tvCategory, tvTitle, tvName, tvDigest, tvLikecCount;
        private ImageView ivIcon, ivImage,ivLike;
        public boolean isLike;

        public Holder(View v) {
            super(v);
            tvCategory = (TextView) v.findViewById(R.id.tv_category_recycler);
            tvTitle = (TextView) v.findViewById(R.id.tv_title_recycler);
            tvName = (TextView) v.findViewById(R.id.tv_author_recycler);
            tvDigest = (TextView) v.findViewById(R.id.tv_digest_recycler);
            tvLikecCount = (TextView) v.findViewById(R.id.tv_count_recycler);
            ivIcon = (ImageView) v.findViewById(R.id.iv_icon_recycler);
            ivImage = (ImageView) v.findViewById(R.id.iv_image_recycler);
            ivLike = (ImageView) v.findViewById(R.id.iv_like_recycler);
        }
    }

    private OnItemNameClickListener nameClickListener;
    private OnItemTitleClickListener titleClickListener;
    private OnItemDigestClickListener digestClickListener;
    private OnItemLikeCountClickListener likeClickListener;
    private OnItemIconClickListener iconClickListener;
    private OnItemLikeIconClickListener likeIconClickListener;
    private OnItemImageClickListener imageClickListener;
    public void setOnItemNameClickListener(OnItemNameClickListener listener){
        this.nameClickListener = listener;
    }

    public void setOnItemTitltClickListener(OnItemTitleClickListener listener){
        this.titleClickListener = listener;
    }
    public void setOnItemDigestClickListener(OnItemDigestClickListener listener){
        this.digestClickListener = listener;
    }
    public void setOnItemLikeCountClickListener(OnItemLikeCountClickListener listener){
        this.likeClickListener = listener;
    }
    public void setOnItemIconClickListener(OnItemIconClickListener listener){
        this.iconClickListener = listener;
    }
    public void setOnItemLikeIconClickListener(OnItemLikeIconClickListener listener){
        this.likeIconClickListener = listener;
    }
    public void setOnItemImageClickListener(OnItemImageClickListener listener){
        this.imageClickListener = listener;
    }
    private void setmHolderListener(final Holder mHolder, final int position) {
        mHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nameClickListener != null)
                    nameClickListener.onItemClick(position,mHolder.tvName,mHolder);
            }
        });
        mHolder.ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (likeIconClickListener != null)
                    likeIconClickListener.onItemClick(position,mHolder.tvLikecCount,mHolder.ivLike,mHolder);
            }
        });
        mHolder.tvLikecCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (likeClickListener != null)
                    likeClickListener.onItemClick(position,mHolder.ivLike,mHolder.tvLikecCount,mHolder);
            }
        });
        mHolder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageClickListener != null)
                    imageClickListener.onItemClick(position,mHolder.ivImage,mHolder);
            }
        });
        mHolder.tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleClickListener != null)
                    titleClickListener.onItemClick(position,mHolder.tvTitle,mHolder);
            }
        });
        mHolder.ivIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iconClickListener != null)
                    iconClickListener.onItemClick(position,mHolder.ivIcon,mHolder);
            }
        });
        mHolder.tvDigest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (digestClickListener != null)
                    digestClickListener.onItemClick(position,mHolder.tvDigest,mHolder);
            }
        });
    }
}
